package cn.tedu.micro.demo.rabbit.amqp;

import cn.tedu.micro.demo.rabbit.config.DeclareConfiguration;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@Slf4j
public class AmqpConsumer {
    //监听queue01方法,获取消息 进行消费
    @RabbitListener(queues={"queue01"})
    public void consume(String body, Message message, Channel channel){
        log.info("开始消费");
        //1.打印消息体
        log.info("消费消息:{}",body);
        //2.打印消息属性 content-type
        log.info("content-type:{}",message.getMessageProperties().getContentType());
        //3.根据消费结果 反馈成功
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    //监听另一个队列
    @RabbitListener(queues={DeclareConfiguration.DELAY_Q})
    public void consumeDelay(String body,Message message,Channel channel){
        log.info("开始消费");
        log.info("消费消息:{}",body);
        log.info("content-type:{}",message.getMessageProperties().getContentType());
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
